package com.yeexun.controller;

import com.yeexun.domain.RecTo;
import com.yeexun.domain.Recording;
import com.yeexun.service.RecordingService;
import com.yeexun.util.Result;
import com.yeexun.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author rookie
 */
@RestController
@RequestMapping("/rec")
public class RecordingController {

    @Autowired
    private RecordingService recordingService;

    /**
     * 根据月份查询
     * @param month
     * @param id
     * @return
     */
    @RequestMapping("/getMonth")
    public Result queryByMonth(Integer id,String month){
        System.out.println(month);
        try {
            List<RecTo> recTos = recordingService.queryByMonth(id,month);

            return ResultGenerator.genSuccessResult(recTos);
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("查询失败了");
        }
    }


    /**
     * 根据年份查询
     * @param year
     * @param id
     * @return
     */
    @RequestMapping("/getYear")
    public Result queryByYear(Integer id, String year){
        try {
            List<RecTo> recTos = recordingService.queryByYear(id,year);

            return ResultGenerator.genSuccessResult(recTos);
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("查询失败了");
        }
    }

    /**
     * 添加
     * @param recording
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody Recording recording){
        System.out.println(recording);
        try {
            Result save = recordingService.save(recording);
            return save;
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("添加失败了");
        }
    }

    /**
     * 计算余额
     * @param userId
     * @return
     */
    @RequestMapping("/balance")
    public Result balance(Integer userId){
        try {
            BigDecimal sumMoney = recordingService.selectSumMoney(userId);

            return ResultGenerator.genSuccessResult(sumMoney);
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("余额计算失败");
        }
    }

    /**
     * 查询月账单
     * @param userId
     * @param month
     * @return
     */
    @RequestMapping("/queryMonthBill")
    public Result queryMonthBill(Integer userId,String month){

        try{
            System.out.println(month);
            List<Recording> recordings = recordingService.queryMonth(userId, month);

            return ResultGenerator.genSuccessResult(recordings);
        }catch(Exception e) {
            e.printStackTrace();

            return ResultGenerator.genFailResult("查询失败");
        }
    }

    /**
     * 删除账单
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result deleteBill(Integer id){
        try {
            Result result = recordingService.deleteBill(id);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("删除失败了");
        }
    }
 }
